<template>
  <d2-container>
    <div style="margin: 20px">
      <div style="margin: 10px 0">
        <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="project_name"></el-input>
        <el-button style="margin-left: 10px" type="primary" @click="getProjects">搜索</el-button>
        <el-button style="margin-left: 10px" type="primary" @click="reset">重置</el-button>
        <el-button style="float:right" type="primary" @click="$router.push('/project/create')">创建项目</el-button>
      </div>
      <div style="margin: 10px 0">
        <el-row v-for="item in projects" :key="item.id">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>{{ item.name }}</span>
              <el-popconfirm popper-class="popper" title="确认删除该项目吗?" @confirm="deleteProject(item.id,item.children)">
                <el-button style="float: right; margin-left:10px; padding: 5px 0; color: rgba(197,69,102,0.68)"
                           type="text" slot="reference">删除
                </el-button>
              </el-popconfirm>
              <el-button style="float: right; padding: 5px 0" type="text"
                         @click="handleUpdate(item)">编辑
              </el-button>
            </div>
            <el-descriptions class="margin-top" :column="2" border>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-user"/>
                  项目名称
                </template>
                {{ item.name }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-mobile-phone"/>
                  项目类型
                </template>
                <el-tag size="small" v-if="item.type===0">
                  图像分类
                </el-tag>
                <el-tag size="small" v-if="item.type===1">
                  目标检测
                </el-tag>
                <el-tag size="small" v-if="item.type===2">
                  图像分割
                </el-tag>
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-tickets"/>
                  项目描述
                </template>
                {{ item.description }}
              </el-descriptions-item>
            </el-descriptions>
          </el-card>
        </el-row>
      </div>

      <!--分页插件-->
      <div style="margin: 10px 0">
        <el-card class="card" v-if="totalPage >= 0">
          <div>
            <el-pagination
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="[1, 2, 3]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalPage">
            </el-pagination>
          </div>
        </el-card>
      </div>

      <!-- 修改项目信息对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="40%">
          <el-form ref="form" :model="form" :rules="rules" size="medium" label-width="50px">
            <el-form-item label-width="80px" label="项目名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入项目名称" :style="{width: '50%'}"/>
            </el-form-item>
            <el-form-item label-width="80px" label="项目类型" prop="type">
              <el-tag size="small" v-if="form.type===0">
                图像分类
              </el-tag>
              <el-tag size="small" v-if="form.type===1">
                目标检测
              </el-tag>
              <el-tag size="small" v-if="form.type===2">
                图像分割
              </el-tag>
            </el-form-item>
            <el-form-item label-width="80px" label="项目描述" prop="description">
              <el-input type="textarea" v-model="form.description" placeholder="请输入项目描述" :style="{width: '50%'}"/>
            </el-form-item>
          </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submit">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </d2-container>
</template>

<script>

import {DelProObj, GetProList, UpdateObj} from './api'

export default {
  name: 'index',
  data () {
    return {
      projects: [],
      project_name: '',
      totalPage: 0,
      currentPage: 1,
      pageSize: 3,
      form: {},
      rules: {
        name: [
          { required: true, message: '请输入项目名称', trigger: 'blur' },
          { min: 3, max: 20, message: '项目名称需要多于3个字符但不能超过20个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入项目描述', trigger: 'blur' },
          { min: 5, max: 200, message: '项目描述需要多于5个字符但不能超过200个字符', trigger: 'blur' }
        ]
      },
      title: '',
      open: false
    }
  },
  methods: {
    // 获取所有项目
    getProjects () {
      const query = { name: this.project_name, creator: this.$store.state.d2admin.user.info.user_id }
      query.limit = this.pageSize
      query.page = this.currentPage
      GetProList(query).then(response => {
        this.totalPage = response.data.total
        this.currentPage = response.data.page
        this.projects = response.data.data
      })
    },
    // 重置搜索框
    reset () {
      this.project_name = ''
      this.getProjects()
    },
    // 修改项目信息
    handleUpdate (item) {
      this.open = true
      this.title = '修改项目信息'
      this.form = Object.assign({}, item)
    },
    // 提交修改项目信息
    submit (id) {
      this.$refs.form.validate(valid => {
        if (valid) {
          UpdateObj(this.form).then(response => {
            this.$message.success('编辑项目成功！')
            this.open = false
            this.getProjects()
          }).catch(error => {
            this.$message.error('编辑项目失败！')
            console.log(error)
          })
        }
      })
    },
    // 取消修改项目信息
    cancel () {
      this.open = false
    },
    // 删除项目
    deleteProject (id, children) {
      for (let i = 0; i < children.length; i++) {
        if (children[i].status === 1) {
          this.$message.error('项目下有模型正在训练，无法删除！')
          return
        }
      }
      DelProObj(id).then(response => {
        this.$message.success('删除项目成功！')
        this.getProjects()
      }).catch(error => {
        this.$message.error('删除项目失败！')
        console.log(error)
      })
    },
    // 每页条数改变
    handleSizeChange (val) {
      this.pageSize = val
      this.getProjects()
    },
    // 翻页
    handleCurrentChange (val) {
      this.currentPage = val
      this.getProjects()
    }
  },
  created: function () {
    this.getProjects()
  }
}
</script>

<style scoped>
</style>
